3 Andrés Mejía-Posada (andmej@gmail.com)
27 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
28 #define For(i, a, b) for (int i=(a); i<(b); ++i)
29 #define D(x) cout << #x " is " << x << endl
37 for (int i
=0; i
<rows
; ++i
){
38 for (int j
=0; j
<cols
; ++j
){
39 d
[i
][j
] = INT_MAX
>> 1;
43 deque
<pair
<int, int> > q
;
44 q
.push_front(make_pair(si
, sj
));
46 int i
= q
.front().first
, j
= q
.front().second
, w
= d
[i
][j
];
48 if (w
> d
[i
][j
]) continue;
49 if (i
== ei
&& j
== ej
) return w
;
50 static const int di
[] = {-1, -1, +0, +1, +1, +1, +0, -1};
51 static const int dj
[] = {+0, +1, +1, +1, +0, -1, -1, -1};
52 for (int k
=0; k
<8; ++k
){
53 int ni
= i
+ di
[k
], nj
= j
+ dj
[k
];
54 if (0 <= ni
&& ni
< rows
&& 0 <= nj
&& nj
< cols
){
55 int w_extra
= mat
[i
][j
] - '0' != k
;
56 if (w
+ w_extra
< d
[ni
][nj
]){
57 d
[ni
][nj
] = w
+ w_extra
;
59 q
.push_back(make_pair(ni
, nj
));
61 q
.push_front(make_pair(ni
, nj
));
73 for (int i
=0; i
<rows
; ++i
){
74 for (int j
=0; j
<cols
; ++j
){
81 cin
>> si
>> sj
>> ei
>> ej
;
82 si
--, sj
--, ei
--, ej
--;
83 cout
<< bfs() << endl
;